<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>首页</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
  </head>
  <body>
    <h1>首页</h1>
    <button id="logout">退出登录</button>

    <script>
      $(function () {
        $.get('/api/username', (res) => {
          if (res.status !== 0) {
            alert('您尚未登录，请登录后在执行此操作')
            location.href = './login.html'
          } else {
            alert('欢迎您,' + res.username)
          }
        })

        $('#logout').on('click', () => {
          $.post('/api/logout', (res) => {
            if (res.status === 0) {
              location.href = './login.html'
            }
          })
        })
      })
    </script>
  </body>
</html>
